package com.phs.main.security.xss;


import com.phs.main.common.utils.MyStringUtils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import java.util.HashMap;
import java.util.Map;

/**
 * Filter配置
 */
@Configuration
public class FilterConfig {

	private final String excludes = "";

	@SuppressWarnings({"rawtypes", "unchecked"})
	@Bean
	public FilterRegistrationBean xssFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setDispatcherTypes(DispatcherType.REQUEST);
		registration.setFilter(new XssFilter());
		String urlPatterns = "/*";
		registration.addUrlPatterns(MyStringUtils.split(urlPatterns, ","));
		registration.setName("xssFilter");
		registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
		Map<String, String> initParameters = new HashMap<>();
		initParameters.put("excludes", excludes);
		initParameters.put("enabled", "true");
		registration.setInitParameters(initParameters);
		return registration;
	}

}
